// pages/draw/generate/generate.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    timer:null,
    id: '',
    typeid: '',
    sizeid: '',
    styleid: '',
    content: '',
    active: 0,
    img: ''
  },
  submit(e) {
    let num = e.currentTarget.dataset.num;
    app.util.request({
      url: '/huahua/change',
      data: {
        id: this.data.id,
        action: 'UPSCALE',
        index: num
      }
    }).then(res => {
      app.util.message(res.message)
      setTimeout(() => {
        wx.navigateTo({
          url: '../look/look?id=' + res.data
        })
      }, 1000);
    })
  },
  submit2(e) {
    let num = e.currentTarget.dataset.num;
    app.util.request({
      url: '/huahua/change',
      data: {
        id: this.data.id,
        action: 'VARIATION',
        index: num
      }
    }).then(res => {
      app.util.message(res.message)
      setTimeout(() => {
        wx.navigateTo({
          url: '../look/look?id=' + res.data
        })
      }, 1000);
    })
  },
  copy() {
    wx.setClipboardData({
      data: this.data.content,
      success(res) {
        console.log('已复制')
      }
    })
  },
  refresh(){
    this.getImg2(this.data.id)
  },
  main(){
    wx.reLaunch({
      url: '/pages/user/index',
    })
  },
  draw(){
    wx.navigateTo({
      url: '/pages/draw/index?typeid='+this.data.typeid+'&sizeid='+this.data.sizeid+'&styleid='+this.data.styleid+'&content='+this.data.content
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id: options.id,
      typeid: options.typeid,
      sizeid: options.sizeid,
      styleid: options.styleid,
      content: options.content
    })
    this.getImg(options.id)
    // this.data.timer = setInterval(() => {
    //   this.getImg(options.id)
    // }, 4000); 
    
  },
  getImg(id) {
    let that = this;
    app.util.request({
      url: '/huahua/get_result',
      data: {
        id: id,
        loading: 'AAA'
      }
    }).then(res => {
      if (res.data.state == 1) {
        // clearInterval(this.data.timer); //关闭
        wx.hideLoading();
        this.setData({
          img: res.data.images[0]
        })
      } else if(res.data.state == 2){
        // clearInterval(this.data.timer); //关闭
        wx.hideLoading();
        app.util.message('生成失败，费用已退，请返回重试', 'error', function () {
          wx.navigateBack()
        })
      }else{
        wx.showLoading({
          title: '生成中不要离开',
          mask: true
        });
        setTimeout(() => {
          that.getImg(that.data.id)
        }, 3000);
      }
    })
  },
  getImg2(id) {
    let that = this;
    app.util.request({
      url: '/huahua/restart',
      data: {
        id: id,
        loading: 'AAA'
      }
    }).then(res => {
      if (res.data.state == 1) {
        // clearInterval(this.data.timer); //关闭
        wx.hideLoading();
        this.setData({
          img: res.data.images[0]
        })
      } else if(res.data.state == 2){
        // clearInterval(this.data.timer); //关闭
        wx.hideLoading();
        app.util.message('生成失败，费用已退，请返回重试', 'error', function () {
          wx.navigateBack()
        })
      }else{
        wx.showLoading({
          title: '生成中不要离开',
          mask: true
        });
        setTimeout(() => {
          that.getImg(that.data.id)
        }, 3000);
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    if (this.data.timer) {
      //如果定时器还在运行 或者直接关闭，不用判断
      clearInterval(this.data.timer); //关闭
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    if (this.data.timer) {
      //如果定时器还在运行 或者直接关闭，不用判断
      clearInterval(this.data.timer); //关闭
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})